package uumoi

import grails.plugin.springsecurity.annotation.Secured
import grails.transaction.Transactional

import static org.springframework.http.HttpStatus.*

@Transactional(readOnly = true)
@Secured(['ROLE_USER'])
class CollectionController {

	static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

	def index() {
		if (params.brandId) {
			if (Brand.canAccess(params.brandId as Long, Account.get(session.account)))
				session.brand = params.brandId as Long
		}

		if (params.regionId) {
			if (Region.canAccess(params.regionId as Long, Account.get(session.account)))
				session.region = params.regionId as Long
		}

		if (params.channelId) {
			if (Channel.canAccess(params.channelId as Long, Account.get(session.account)))
				session.channel = params.channelId as Long
		}

		def (account, brand, region, channel) = selections()
		def (brands, regions, channels) = selectionLists(account, brand, region)

		def (activeCollections, futureCollections) = searchCollections(account, brand, region, channel)

		[brands: brands, regions: regions, channels: channels, activeCollections: activeCollections, futureCollections: futureCollections]
	}

	private selections() {
		return [Account.get(session.account), session.brand ? Brand.get(session.brand) : null, session.region ? Region.get(session.region) : null, session.channel ? Channel.get(session.channel) : null]
	}

	static selectionLists(Account account, Brand brand, Region region) {
		def brands, regions, channels = []

		brands = account.brands

		if (brands.size() == 1)
			regions = brands[0].regions
		else {
			if (brand)
				regions = brand.regions
		}

		if (regions && regions.size() == 1)
			channels = Channel.findByRegionAndBrand(regions[0], brand)

		if (region && brand)
			channels = Channel.findByRegionAndBrand(region, brand)

		return [brands, regions, channels]
	}

	static searchCollections(Account account, Brand brand, Region region, Channel channel) {
		return [Collection.findActiveCollections(account, brand, region, channel), Collection.findFutureCollections(account, brand, region, channel)]
	}

	def show(Collection collectionInstance) {
		respond collectionInstance
	}

	def create() {
		respond new Collection(params)
	}

	def imagetest() {

	}

	@Transactional
	def save(Collection collectionInstance) {
		if (collectionInstance == null) {
			notFound()
			return
		}

		if (collectionInstance.hasErrors()) {
			respond collectionInstance.errors, view:'create'
			return
		}

		collectionInstance.save flush:true

		request.withFormat {
			form multipartForm {
				flash.message = message(code: 'default.created.message', args: [message(code: 'collectionInstance.label', default: 'Collection'), collectionInstance.id])
				redirect collectionInstance
			}
			'*' { respond collectionInstance, [status: CREATED] }
		}
	}

	def edit(Collection collectionInstance) {
		respond collectionInstance
	}

	@Transactional
	def update(Collection collectionInstance) {
		if (collectionInstance == null) {
			notFound()
			return
		}

		if (collectionInstance.hasErrors()) {
			respond collectionInstance.errors, view:'edit'
			return
		}

		collectionInstance.save flush:true

		request.withFormat {
			form multipartForm {
				flash.message = message(code: 'default.updated.message', args: [message(code: 'Collection.label', default: 'Collection'), collectionInstance.id])
				redirect collectionInstance
			}
			'*'{ respond collectionInstance, [status: OK] }
		}
	}

	@Transactional
	def delete(Collection collectionInstance) {

		if (collectionInstance == null) {
			notFound()
			return
		}

		collectionInstance.delete flush:true

		request.withFormat {
			form multipartForm {
				flash.message = message(code: 'default.deleted.message', args: [message(code: 'Collection.label', default: 'Collection'), collectionInstance.id])
				redirect action:"index", method:"GET"
			}
			'*'{ render status: NO_CONTENT }
		}
	}

	protected void notFound() {
		request.withFormat {
			form multipartForm {
				flash.message = message(code: 'default.not.found.message', args: [message(code: 'collectionInstance.label', default: 'Collection'), params.id])
				redirect action: "index", method: "GET"
			}
			'*'{ render status: NOT_FOUND }
		}
	}
}
